package com.jlcindia.hibernate;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Filter;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.jlcindia.util.HibernateUtil;

public class Lab49 {
	public static void main(String args[]) {
		Transaction tx = null;
		try {
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();

			System.out.println("1. All Customers");
			displayAllCustomers(session);
			
			System.out.println("2. Show Active Customers");
			Filter filter = session.enableFilter("StatusFilter");
			filter.setParameter("statusParam", "Active");
			displayAllCustomers(session);
			
			System.out.println("3. Show InActive Customers");
			filter.setParameter("statusParam", "Active1");
			displayAllCustomers(session);
			
			tx.commit();
			session.close();

		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace();
		}
	}
	
	public static void displayAllCustomers(Session session){
		Transaction transaction = session.beginTransaction();
		Query q = session.createQuery("from Customer c");
		List<Customer> clist = q.list();
		for (Customer c : clist) {
			System.out.println(c);
		}
	}
}
